//index.js
//获取应用实例
var app = getApp();
Page({
  data: {
    listData: [],
    page: 1,
    loading: 0,
    lastPage: 0,
    loaded: 0,
    kw:'',
    user_info: {}
  },
  onLoad: function (options) {
    var that = this;
    app.getUserInfo(function (user_info) {
      that.data.user_info = user_info;
      that.initData();
    })

  },
  initData: function (reload = 0) {
    if (reload == 1) {
      this.setData({
        listData: [],
        page: 1,
        loading: 0,
        lastPage: 0,
        loaded: 0,
      });
    }
    this.getList();

  },

del:function(e){
    var that = this;
    var idx = e.currentTarget.dataset.key;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
    title: '提示',
    content: '确认删除吗？',
    success: function(res) {
          if (res.confirm) {
            //删除评论
            app.api("api/community_rooms/del",{
              out_room_id:id,
              method: 'POST'
              },function(res){
               //删了出错
                if (res.code!=200){
                    app.alert(res.msg);
                }else{
                    wx.showToast({
                      title: '删除成功',
                      icon: 'success',
                      duration: 2000
                    })
                    var listData=that.data.listData;
                    listData[idx]['none']=1;
                    that.setData({ listData: listData});
                }      
            });   
          }
         }
    })
  },
  
  moreAct:function(e){
      var id = e.currentTarget.dataset.id;
      var out_room_id = e.currentTarget.dataset.out_room_id;
      //var key = e.currentTarget.dataset.key;
      var itemListArray = [];
      itemListArray.push({ text: '发送帐单', act: function(){
            wx.navigateTo({
              url: '/pages/tenement/addbills?out_room_id=' + out_room_id,
            })
      } });
      //if (to_user_id == this.data.user_info.user_id || to_user_id==this.data.data.user_id){
        itemListArray.push({ text: '删除', act: this.del });
     // }
      var itemList = [];
      //console.log(itemListArray);
      for (var i in itemListArray) {
        itemList.push(itemListArray[i].text);
      }
      wx.showActionSheet({
        itemList: itemList,
        success: function (res) {
          console.log(itemListArray[res.tapIndex]);
          itemListArray[res.tapIndex]['act'](e);
        },
        fail: function (res) {
          console.log(res.errMsg)
        }
      })
  },


  //提醒列表
  getList() {
    //通知提醒 
    var that = this;
    var page = this.data.page;
    this.setData({
      loading: 1
    });
    app.api("api/community_rooms/get", {
      page: page,
      limit: 20,
      kw:this.data.kw,
      community_id: this.data.user_info.community.community_id
    }, function (res) {
      wx.stopPullDownRefresh();
      if (res.code != 200) {
        app.alert(res.msg, function () {
          wx.navigateBack();
        });
        return;
      }
      if (res.data.list.length == 0) {
        that.setData({
          lastPage: 1
        });
      }
      page++;
      if (res.data.list) {
        that.setData({ page: page, loading: 0, 'listData': that.data.listData.concat(res.data.list) });
      }
    });
  },
  onReachBottom: function () {
    this.initData();
  },


  onPullDownRefresh: function () {
    this.initData(1);
  },
  onShow:function(){
    var reload = app.getPageData("tenement/index");
    if (reload==1){
      this.initData(1);
      app.setPageData("tenement/index", { reload:0});
    }
  },
  setsearchkw: function (event) {
    this.data.kw = event.detail.value;
    // console.log(this.data.kw);
  },
  search: function () {
    this.initData(1);
  }

})
